package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.mail.MailUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.RoomType;
import com.zzyl.mapper.NursingPlanProMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import com.zzyl.vo.RoomTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Transactional
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingPlanProMapper planProMapper;


    @Override
    public PageResponse findQuery(String name, Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectPage(name, status);
        //将 page集合 转成 list集合
        List<NursingProjectVo> list = page.getResult();
       /* for (NursingProjectVo vo : list) {
            Long id = vo.getId();
            Long count = planProMapper.countFind(id);
            vo.setCount(count);
        }*/
        // 从上述集合中获取集合中每个对象的ID集合
        List<Long> collect = list.stream().map(a -> a.getId()).collect(Collectors.toList());
        //获取每个项目id被使用的次数
        List<Map> mapList = planProMapper.countFind(collect);
        //toMap两个参数，一个参数是key, 一个参数是value
        Map<Long, Long> map = mapList.stream().collect(Collectors.toMap(
                mp->Long.valueOf(mp.get("pid").toString()),
                mp->Long.valueOf(mp.get("ct").toString()))
        );
       /* for (NursingProjectVo vo : list) {
            vo.setCount(map.getOrDefault(vo.getId(),0L));
        }
*/
        list.stream().forEach(vo->
                vo.setCount(map.getOrDefault(vo.getId(),0L)));
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(dto, nursingProject);
        nursingProjectMapper.instert(nursingProject);
    }

    @Override
    public NursingProjectVo findById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if (nursingProject == null) {
            return null;
        }
        NursingProjectVo vo = new NursingProjectVo();

        BeanUtil.copyProperties(nursingProject, vo);
        return vo;
    }

    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        if (dto == null) {
            return;
        }
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(dto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void deletePro(Long id) {
        NursingProjectVo vo = findById(id);
        Integer status = vo.getStatus();
        if(status == 1){
            nursingProjectMapper.deletePro(id);
        }else {
            return;
        }

    }

    @Override
    public List<NursingProjectVo> findQueryList() {
        return nursingProjectMapper.selectAll();
    }

}
